package cn.kgc.layui.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String path = request.getServletPath();
        String url = request.getRequestURI();
//        System.out.println(path);
//        System.out.println("-----");
//        System.out.println(url);
        // 如果不需要过滤的静态文件，直接放行
        if (path.endsWith(".css") || path.endsWith(".js")
                || path.endsWith(".jpg")
                || path.endsWith(".png")
                || path.endsWith(".woff2")
                || path.endsWith(".gif")
                || path.endsWith(".ico")
                || path.endsWith("index.jsp")
                || path.endsWith("/validate")
                || url.indexOf("resource") > 0 || url.indexOf("note") > 0
                || url.endsWith("/login/loginUI") || url.endsWith("/login/doLogin")

        ) {
            filterChain.doFilter(request, response);
            return;
        } else {
            if (checkUser(request, response)) {
                filterChain.doFilter(request, response);// 如果用户已经登录，就放行
                return;
            } else {
                // 没有登录，重定向到登录页面
                String contextPath = getContextPath(request);
                response.sendRedirect(contextPath+ "/login/loginUI");
                return;
            }
        }
    }

    protected String getContextPath(HttpServletRequest request) {
        String contextPath = request.getContextPath();

        if ((contextPath == null) || (contextPath.equals("/"))) {
            return "";
        }
        return contextPath;
    }

    private boolean isInArray(String path, String nofilterFiles[]) {
        for (int i = 0; i < nofilterFiles.length; i++) {
            String nofilterFile = nofilterFiles[i];
            if (nofilterFile.equals(path)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkUser(HttpServletRequest request,
                              HttpServletResponse response) throws IOException {

        //2.校验session
        HttpSession session = request.getSession(false);// 如果不存在返回空
        if (session == null) {
            return false;
        }
        //3.校验登录用户
        Object obj = session.getAttribute("manager");
        if (obj == null) {
            return false;
        }

        return true;
    }

    @Override
    public void destroy() {

    }
}
